package com.efuture.androidmvvmdemo.view.ui

import android.content.Intent
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.efuture.androidmvvmdemo.R
import com.efuture.androidmvvmdemo.base.BaseActivity
import com.efuture.androidmvvmdemo.databinding.ActivityLoginBinding
import com.efuture.androidmvvmdemo.listener.LoginListener
import com.efuture.androidmvvmdemo.viewmodel.LoginViewModel


class LoginActivity : BaseActivity() {
    private lateinit var binding:ActivityLoginBinding
    private lateinit var loginModel: LoginViewModel
    private lateinit var loginListener :LoginListener
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.hide()
        binding = DataBindingUtil.setContentView<ActivityLoginBinding>(this,R.layout.activity_login)
        loginModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        loginListener = LoginListener(loginModel,this)
        click()
        observeData()
    }

    private fun observeData() {
        loginModel.loginData.observe(this,{
            loginListener.dealLoginResult(it)
        })
    }

    private fun click() {
        binding.btLogin.setOnClickListener {
            loginListener.loginUser(binding.etUsername.text.toString(),binding.etPassword.text.toString())
        }
        binding.btRegister.setOnClickListener {
            startActivity(Intent(this,RegisterActivity::class.java))
        }
    }
}